package com.itholmes.usercenter.exception;

import com.itholmes.usercenter.common.BaseResponse;
import com.itholmes.usercenter.common.ErrorCode;
import com.itholmes.usercenter.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: xuyanbo
 * @description: 全局异常处理器
 * @date: 2022/7/30 13:15
 */
//原理是SpringAOP的作用
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //捕获自定义异常
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException: " + e.getMessage() , e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }



}
